<?php
// 引入请求函数 
require_once("request.php");

// 要使用Storage
use sinacloud\sae\Storage as Storage;

define("APPID","wx9304369530ffc11d");
define("SECRET","31977f083f40a72c9c2faa100c487ebb");
// 获取新的access_token信息的函数
function  getNewAccess_TokenInfo(){
  $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".APPID."&secret=".SECRET;
// 调用函数获取access_token的信息
  $response = httpGet($url);
return $response;
}

//获取access_token的函数
function getAccessToken(){
  // 让$responseObj等于读取Storage中access_token的返回值
  $responseObj = readAccessTokenInfoFromStorage();
  if ($responseObj == false) {
    // 如果没有 就获取新的access_token  其实是获取到一个json串
      $res = getNewAccess_TokenInfo();
    // 解析json串
      $obj = json_decode($res);
      // 获取access_token和expires_in
      $access_token = $obj->access_token;
      $expires_in = $obj->expires_in;
      // 现获取当前的时间   1970年1月1日0时0分0秒到现在有多少秒
      $current_time = time();
      //得到过期的时间点
      $expires_time = $current_time + $expires_in - 200;
      // 存储到
      // 创建一个空数组的对象  （强转成对象）
      $savedObj = (object)array();
      // 将我们要存的东西存入
      $savedObj->access_token = $access_token;
      $savedObj->expires_time = $expires_time;
      // 转换成json串
      $saveString = json_encode($savedObj);
      // 创建一个Storage对象
      $s = new Storage();
      // 获取bucket
      $bucket = $s->getBucket("bucket");
      // 如果bucket不存在 就创建bucket
      if ($bucket == false) {
        // 创建bucket
          $s->putBucket("bucket",".r:*");
      }
      // 写入操作
      $s->putObject($saveString,"bucket","access_token.txt",array(),array("Content-type"=>"text/plain;charset=utf-8"));
      echo $access_token."bbb";
      echo "<hr>";
      return $access_token;
  }else {
    // 如果存有 便获取到access_token 和expires_in
      $access_token = $responseObj->access_token;
      $expires_time = $responseObj->expires_time;
      echo "<hr>";
      echo $expires_time."--------";
      // 获取当前的时间
      $current_time = time();
      echo "<hr>";
      echo $expires_time."expires_time";
      echo "<hr>";
      // 判断是否过期
      if ($current_time >= $expires_time) {
        // 如果已经过期了 重新获取
        $res = getNewAccess_TokenInfo();
        $obj = json_decode($res);
        // 获取access_token  和 expires_in
        $access_token =  $obj->access_token;
        $expires_in = $obj->expires_in;
        $expires_time = $current_time + $expires_in - 200;
        // 写入
        $savedObj = (object)array();
        $savedObj->access_token  = $access_token;
        $savedObj->expires_time = $expires_time;
        // 将对象转换成json串
        $saveString = json_encode($savedObj);
        // 写入bucket
        $s= new Storage();
        // 现获取bucket
        $bucket = $s->getBucket("bucket");
        if ($bucket == false) {
          $s->putBucket("bucket",".r:*");
        }
        // 写入  
        $s->putObject($saveString,"bucket","access_token.txt",array(),array("Content-type"=>"text/plain;charset=utf-8"));
        echo $access_token."aaa";
        echo "<hr>";
        return $access_token;
      }else {
        echo $access_token."ccc";
        echo "<hr>";
        return $access_token;
      }
  }
}

function readAccessTokenInfoFromStorage(){
  // 先判断bucket中有没有存access_token
  // 如果存储了并且没有过期 直接使用
  // 如果过期了 就重新获取 并存储
 $s = new Storage();
 // 获取bucket  @符号取消用户警告
 $bucket = @$s->getBucket("bucket");
if ($bucket == false) {
    return false;
}else {
  $json = $s->getObject("bucket","access_token.txt");
  // 将json转换成$object返回   读取出的文件中有几个部分 其中内容部分的下标为body
  return json_decode($json->body);
  }
}



// 创建自定义菜单
function  createMenu(){
  // 先获取access_token
  $access_token = getAccessToken();
    echo $access_token;
  // 将access_token传入网址中
  $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token={$access_token}";
  // post请求所需的参数
  $postdata = '{
      "button":[
        {
          "name":"点击事件",
          "sub_button":[
            {
              "type":"click",
              "name":"发送文字",
              "key":"sendText"
            },{
              "type":"click",
              "name":"发送图片",
              "key":"sendImage"
            },{
              "type":"click",
              "name":"发送语音",
              "key":"sendVoice"
            },{
              "type":"click",
              "name":"发送视频",
              "key":"sendVideo"
            },{
              "type":"click",
              "name":"发送图文",
              "key":"sendArticle"
            }
          ]
        },{
          "name":"扫码和相册",
          "sub_button":[
            {
              "type":"scancode_push",
              "name":"扫码推事件",
              "key":"scancode"
            },{
              "type":"scancode_waitmsg",
              "name":"扫码带提示",
              "key":"scancode_waitmsg"
            },{
              "type":"pic_sysphoto",
              "name":"系统拍照发图",
              "key":"pic_sysphoto"
            },{
              "type":"pic_photo_or_album",
              "name":"拍照或相册发图",
              "key":"pic_photo_or_album"                
              },{
              "type":"pic_weixin",
              "name":"微信相册发图",
              "key":"pic_weixin"
              }]
            },{
      	 	"name":"其他事件",
      	 	"sub_button":[
      	 	{
      	 		"type":"view",
      	 		"name":"蓝鸥科技官网",
      	 		"url":"http://xiaojidunmogu.applinzi.com/shuqian2/shuqian.html"
      	 	},
      	 	{
      	 		"type":"location_select",
      	 		"name":"发送位置",
      	 		"key":"location"
      	 	}]
      	 }]}';
            // 得到返回值
      $response  = httpPost($url,$postdata);
      return $response;
}





function getMenuInfo(){
  $access_token = getAccessToken();
  $url = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token={$access_token}";
  return httpGet($url);
}






function deleteMenu(){
  $access_token = getAccessToken();
  $url = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token={$access_token}";
  return httpGet($url);
}



// 上传临时素材 
function uploadMedia($type,$media){
    // 获取access_token 
  $access_token = getAccessToken();
  // 做网络请求
  $url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token={$access_token}&type={$type}";
  return httpPost($url,$media);
}


// realpath 指的是一个文件的完整路径 
// 只要把文件放进数组中 会默认认成form-data格式
$media = array("media"=>"@".realpath("1.png"));
$type = "image";
// 调用函数
echo uploadMedia($type,$media);



//创建自定义菜单
function createCustomMenu($data){
  $access_token = getAccessToken();
  $url = "https://api.weixin.qq.com/cgi-bin/menu/addconditional?access_token={$access_token}";
  return httpPost($url,$data);
}
$data = '{
  "button":[
  { 
      "type":"click",
      "name":"今日歌曲",
      "key":"music" 
  },
  { 
    "name":"菜单",
    "sub_button":[
    { 
      "type":"view",
      "name":"搜索",
      "url":"http://www.baidu.com/"
    },
    {
      "type":"view",
      "name":"视频",
      "url":"http://v.qq.com/"
    },
    {
      "type":"click",
      "name":"亲一个",
      "key":"kiss"
    }]
 }],
"matchrule":{
  "client_platform_type":"2"
  }
}';

$response2 = createCustomMenu($data);

$res = json_decode($response2);
$menuid = $res->menuid;
echo $menuid;

function deleteCustomMenu($data){
  $access_token = getAccessToken();
  $url = "https://api.weixin.qq.com/cgi-bin/menu/delconditional?access_token={$access_token}";
  return httpPost($url,$data);
}

$data2 ="{'menuid':'{$menuid}'}";


echo deleteMenu();
echo "<hr>aaaaaaaaaaaaaa";
echo createMenu();

 ?>
